package com.bfxy.rabbitmq.exchange.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * Created by deng_wb on 2020/3/9
 */
public class Producer4TopicExchange {

    public static void main(String[] args)throws Exception {
        // 1. 创建一个ConnectionFactory,并进行配置
        ConnectionFactory connectionFactory = new ConnectionFactory();

        connectionFactory.setHost("192.168.58.3");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        // 2. 通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();

        // 3. 通过connection创建一个Channel
        Channel channel = connection.createChannel();

        String exchangeName = "test_topic_exchange";
        String rountingKey1 = "user.save";
        String rountingKey2 = "user.update";
        String rountingKey3 = "user.delete.abc";

        String msg = "Hello World RabbitMQ 4 Topic Exchange Message ... ";
        channel.basicPublish(exchangeName,rountingKey1,null,msg.getBytes());
        channel.basicPublish(exchangeName,rountingKey2,null,msg.getBytes());
        channel.basicPublish(exchangeName,rountingKey3,null,msg.getBytes());

        channel.close();
        connection.close();
    }

}
